﻿using ClosedXML.Excel;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SanJ.Infrastructure.ClosedXML
{
    public class ExcelHelper
    {
        /// <summary>
        /// 根据传入的类型对象,将属性转成Excel第一行
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="sheetName">文件名称</param>
        /// <returns>返回文件路径</returns>
        public static MemoryStream TypeToExcel(List<string> headerList, string sheetName)
        {
            var memoryStream = new MemoryStream();

            using (var workbook = new XLWorkbook())
            {
                var worksheet = workbook.Worksheets.Add(sheetName);

                for (int i = 0; i < headerList.Count(); i++)
                {
                    worksheet.Column(i + 1).Width = 50;
                }
               
                List<IXLCell> headerCells = new List<IXLCell>();
                for (int i = 0; i < headerList.Count(); i++)
                {
                    var cell = worksheet.Cell(1, i + 1);
                    cell.Value = headerList[i];
                    cell.Style.Font.Bold = true;
                    headerCells.Add(cell);
                }

                workbook.SaveAs(memoryStream);
            }

            memoryStream.Position = 0;
            return memoryStream;
        }
    }
}
